﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;
public partial class DayToDayManagement_CMDUPEDIT : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    private bool IsUseColorSize
    {
        get
        {
            return new BLL.SYSPARAM().GetProductUpDownColorSizeConfig();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300100101");
        if (!IsPostBack)
        {
            uptype();
             Bind();
        }
    }
    private void Bind()
    {
        if (Request.QueryString["code"] == null)
        {
            TextBox1.ToolTip = Share.UserValue(0);
            TextBox1.Text = Share.UserValue(1);
            TextBox2.Text = DateTime.Now.ToShortDateString();
            TextBox6.Text = BLL.Common.GetDM("300100101");
            TextBox1.Enabled = false;
            TextBox2.Enabled = false;
            TextBox3.Enabled = false;
            TextBox4.Enabled = false;
            TextBox6.Enabled = TextBox6.Text.Trim() == "" ? true : false;
            ((Button)BUTTONDJ1.Controls[0]).Visible = false;
            ((Button)BUTTONDJ1.Controls[2]).Visible = false;
            //((Button)BUTTONDJ1.Controls[4]).Enabled = false;
            ((Button)BUTTONDJ1.Controls[6]).Enabled = false;
            ((Button)BUTTONDJ1.Controls[8]).Visible = false;
            ((Button)BUTTONDJ1.Controls[10]).Visible = false;
            ((Button)BUTTONDJ1.Controls[12]).Visible = false;
            ((Button)BUTTONDJ1.Controls[14]).Visible = false;
            ((Button)BUTTONDJ1.Controls[16]).Visible = false;
            ((Button)BUTTONDJ1.Controls[18]).Visible = false;
            ((Button)BUTTONDJ1.Controls[20]).Visible = false;
        }
        else
        {
            if (Request.QueryString["code"].ToString().Trim() != "")
            {
                Label1.Text = "编辑商品上架单";
                Model.CMDUP cmd = new BLL.CMDUP().GetModel(Request.QueryString["code"].ToString().Trim());
                DropDownList1.SelectedValue = cmd.CU_UPTYPE;
                TextBox1.ToolTip = cmd.CU_UPER;
                TextBox1.Text = cmd.CU_UPER;
                TextBox2.Text = cmd.CU_UPDATE.ToShortDateString();
                TextBox3.Text = cmd.CU_CHECKER;
                TextBox4.Text = cmd.CU_CHECKDATE == DateTime.MinValue ? "" : cmd.CU_CHECKDATE.ToShortDateString();
                TextBox5.Text = cmd.CU_NOTE;
                TextBox6.Text = Request.QueryString["code"].ToString().Trim();
                TextBox1.Enabled = false;
                TextBox2.Enabled = false;
                TextBox3.Enabled = false;
                TextBox4.Enabled = false;
                TextBox6.Enabled = false;

                if (IsUseColorSize)
                {
                    DataSet ds = new BLL.CMDUPDETAIL().GetCMDUPDETAIL(Request.QueryString["code"].ToString().Trim());
                    ListItem item;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        item = new ListItem();
                        item.Value = ds.Tables[0].Rows[i]["spdm"].ToString() + "~" + ds.Tables[0].Rows[i]["colorCode"].ToString() + "~" + ds.Tables[0].Rows[i]["sizeCode"].ToString();
                        item.Text = ds.Tables[0].Rows[i]["spdm"].ToString() + " " + ds.Tables[0].Rows[i]["spmc"].ToString() + " " + ds.Tables[0].Rows[i]["colorName"].ToString() + " " + ds.Tables[0].Rows[i]["sizeName"].ToString();
                        Select1.Items.Add(item);
                    }
                }
                else
                {
                    DataSet ds = new BLL.CMDUPDETAIL().GetCMDUPDETAILWithoutColorSize(Request.QueryString["code"].ToString().Trim());
                    ListItem item;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        item = new ListItem();
                        item.Value = ds.Tables[0].Rows[i]["spdm"].ToString();
                        item.Text = ds.Tables[0].Rows[i]["spdm"].ToString() + " " + ds.Tables[0].Rows[i]["spmc"].ToString();
                        Select1.Items.Add(item);
                    }
                }


                ((Button)BUTTONDJ1.Controls[2]).Visible = false;
                //((Button)BUTTONDJ1.Controls[4]).Visible = false;
                ((Button)BUTTONDJ1.Controls[8]).Visible = false;
                ((Button)BUTTONDJ1.Controls[10]).Visible = false;
                ((Button)BUTTONDJ1.Controls[12]).Visible = false;
                ((Button)BUTTONDJ1.Controls[14]).Visible = false;
                ((Button)BUTTONDJ1.Controls[16]).Visible = false;
                ((Button)BUTTONDJ1.Controls[0]).Visible = false;
                ((Button)BUTTONDJ1.Controls[18]).Visible = false;
                ((Button)BUTTONDJ1.Controls[20]).Visible = false;
                if (cmd.CU_CHECKER.Trim() != "")
                {
                    ((Button)BUTTONDJ1.Controls[4]).Enabled = false;
                    ((Button)BUTTONDJ1.Controls[6]).Enabled = false;
                }
            }
        }
    }
    private void uptype()
    {
        List<Model.UPTYPE> properties = new BLL.UPTYPE().GetAllList();
        DropDownList1.DataSource = properties;
        DropDownList1.DataTextField = "UT_NAME";
        DropDownList1.DataValueField = "UT_CODE";
        DropDownList1.DataBind();
    }

    public void Save()
    {

        Model.CMDUP cmd = new Model.CMDUP();
        cmd.CU_ID = TextBox6.Text.Trim();
        cmd.CU_UPTYPE = DropDownList1.SelectedValue;
        cmd.CU_UPER = TextBox1.ToolTip;
        cmd.CU_UPDATE = DateTime.Parse(TextBox2.Text.Trim());
        cmd.CU_NOTE = TextBox5.Text;
        cmd.CU_CHECKER = TextBox3.Text;
        cmd.CU_CHECKDATE = TextBox3.Text.Trim() == "" ? DateTime.MinValue : DateTime.Parse(TextBox3.Text.Trim());
        if (Hidden1.Value.Trim().Length > 1)
        {
            string spdmstr = Hidden1.Value.Trim();

            

            if (Request.QueryString["code"] == null)
            {
                if (!Check.CheckPermission("300100101", "00001"))
                {
                    Share.Alert(Page, "无权操作"); return;
                }
                int val = new BLL.CMDUP().P_CMDUP_Save(0, cmd.CU_ID, cmd.CU_UPTYPE, cmd.CU_UPER, cmd.CU_UPDATE, cmd.CU_NOTE, cmd.CU_CHECKER, cmd.CU_CHECKDATE, spdmstr);
                if (val == 1)
                {
                    BLL.Common.ADDLSH("300100101");
                    Share.InsertLog("300100101", "00001");
                }
                else if (val == -2)
                {
                    Share.Alert(Page, "编号已存在"); return;
                }
                else
                {
                    Share.Alert(Page, "操作失败"); return;
                }
            }
            else
            {
                if (Request.QueryString["code"].ToString().Trim() != "")
                {
                    if (!Check.CheckPermission("300100101", "00002"))
                    {
                        Share.Alert(Page, "无权操作"); return;
                    }
                    Model.CMDUP cmdup = new BLL.CMDUP().GetModel(Request.QueryString["code"].ToString().Trim());
                    if (cmdup != null)
                    {
                        if (cmdup.CU_CHECK == 1)
                        {
                            Share.Alert(Page, "此单据已被审核，不允许再次操作！"); return;
                        }
                    }
                    int val = new BLL.CMDUP().P_CMDUP_Save(1, cmd.CU_ID, cmd.CU_UPTYPE, cmd.CU_UPER, cmd.CU_UPDATE, cmd.CU_NOTE, cmd.CU_CHECKER, cmd.CU_CHECKDATE, spdmstr);
                    if (val == 1)
                    {
                        Share.InsertLog("300100101", "00002");
                    }
                    else
                    {
                        Share.Alert(Page, "操作失败"); return;
                    }
                }
            }
            Response.Redirect("CMDUPEDIT.aspx?code=" + cmd.CU_ID);
            
        }
        else
        {
            Share.Alert(Page, "请选择商品"); return;
        }


    }

    public void Audit()
    {
        if (!Check.CheckPermission("300100101", "00010"))
        {
            Share.Alert(Page, "无权操作"); return;
        }
        Model.CMDUP cmdup = new BLL.CMDUP().GetModel(Request.QueryString["code"].ToString().Trim());
        if (cmdup != null)
        {
            if (cmdup.CU_CHECK == 1)
            {
                Share.Alert(Page, "此单据已被审核，不允许再次操作！"); return;
            }
        }
        if (new BLL.CMDUP().P_CMDUP_Audit(Request.QueryString["code"].ToString().Trim(), Share.UserValue(1)))
        {
            Share.InsertLog("300100101", "00010");
            Response.Redirect("CMDUPEDIT.aspx?code=" + Request.QueryString["code"].ToString().Trim());
        }
        else
        {
            Share.Alert(Page, "操作失败");
        }
    }
}
